package com.banciyuan.bcywebview.biz.pwd;

import android.content.Intent;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.api.UserApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.base.view.toast.MyToast;
import com.banciyuan.bcywebview.biz.register.PhoneValidActivity;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.google.gson.Gson;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import de.greenrobot.daoexample.model.PwdToken;

/**
 * Created by squall on 2017/6/29.
 */

public class ChangePwdConfirmPhoneAcitivty extends PhoneValidActivity {


    private void goNext(PwdToken p) {
        Intent intent = new Intent(this, FindPwdStep3Activity.class);
        intent.putExtra(gotoUtil.INTENT_VALUE_ONE, getString(R.string.change_pwd));
        intent.putExtra("pwdtoken", p);
        startActivity(intent);
        finish();
    }

    protected void gotoNextActivity() {
        finish();
    }

    protected void bindMobile() {
        if (is_send) return;
        is_send = true;
        String url = HttpUtils.BASE_URL + UserApi.changePwdVerify();
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.MOBILE, phoneNum));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        paramsList.add(new BcyPair(HttpUtils.CODE, code));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                is_send = false;
                try {
                    if (StatusCodeUtils.dealStatusCode(s, ChangePwdConfirmPhoneAcitivty.this)) {
                        Gson gson = new Gson();
                        JSONObject result = new JSONObject(s);
                        PwdToken pwdToken = gson.fromJson(result.getString(HttpUtils.UPLOAD_DATA), PwdToken.class);
                        goNext(pwdToken);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                is_send = false;
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        mQueue.add(stringRequest);
    }

    @Override
    protected void requestCode() {
        if (has_post) return;
        has_post = true;
        String url = HttpUtils.BASE_URL + UserApi.getRebindMobileCode();
        List<BcyPair> paramsList = new ArrayList<>();
        paramsList.add(new BcyPair(HttpUtils.MOBILE, phoneNum));
        paramsList.add(new BcyPair(HttpUtils.TYPE, "rebind"));
        paramsList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(this).getToken()));
        HashMap<String, String> params = HttpUtils.getData(paramsList);
        Response.Listener succ = new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                has_post = false;
                try {
                    if (StatusCodeUtils.dealStatusCode(s, ChangePwdConfirmPhoneAcitivty.this)) {
                        JSONObject result = new JSONObject(s);
                        MyToast.show(ChangePwdConfirmPhoneAcitivty.this, result.getString(HttpUtils.UPLOAD_DATA));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };

        Response.ErrorListener error = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                has_post = false;
            }
        };
        StringRequestParam stringRequest = new StringRequestParam(Request.Method.POST, url, params, succ, error);
        stringRequest.setRetryPolicy(new DefaultRetryPolicy(15000, 0,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        mQueue.add(stringRequest);
    }
}
